'''
Function:
	定义一些精灵类
'''
import random
import pygame


'''墙类'''
class Wall(pygame.sprite.Sprite):
	def __init__(self, x, y, width, height, color, **kwargs):
		# 自定义精灵类
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.Surface([width, height])#surface对象进行绘图
		self.image.fill(color)
		self.rect = self.image.get_rect()
		self.rect.left = x		#矩形左上角x坐标
		self.rect.top = y		#矩形左上角y坐标


'''食物类'''
class Food(pygame.sprite.Sprite):
	def __init__(self, x, y, width, height, color, bg_color, **kwargs):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.Surface([width, height])
		self.image.fill(bg_color)
		self.image.set_colorkey(bg_color)	# 设置透明色
		pygame.draw.ellipse(self.image, color, [0, 0, width, height])# 绘制椭圆形
		self.rect = self.image.get_rect()
		self.rect.left = x
		self.rect.top = y


'''角色类'''
class Player(pygame.sprite.Sprite):
	def __init__(self, x, y, role_image_path):
		pygame.sprite.Sprite.__init__(self)
		self.role_name = role_image_path.split('/')[-1].split('.')[0]	#实例化对象名称
		self.base_image = pygame.image.load(role_image_path).convert()	#对象图片
		self.image = self.base_image.copy()		#对象图片复制，减少重复路径调用
		self.rect = self.image.get_rect()
		self.rect.left = x
		self.rect.top = y
		self.prev_x = x		#前一时刻位置x
		self.prev_y = y
		self.base_speed = [30, 30]	#基础移动距离
		self.speed = [0, 0]			#实际移动距离
		self.is_move = False
		self.tracks = []		#运动轨迹
		self.tracks_loc = [0, 0]	#当前路段，所走长度
	'''改变速度方向、移动距离'''
	def changeSpeed(self, direction):
		if direction[0] < 0:#向左掉转图像
			self.image = pygame.transform.flip(self.base_image, True, False)
		elif direction[0] > 0:#向右
			self.image = self.base_image.copy()
		elif direction[1] > 0:#向下
			self.image = pygame.transform.rotate(self.base_image, -90)
		elif direction[1] < 0:#向上
			self.image = pygame.transform.rotate(self.base_image, 90)
		self.speed = [direction[0] * self.base_speed[0], direction[1] * self.base_speed[1]]
		return self.speed
	'''更新角色位置'''
	def update(self, wall_sprites, gate_sprites):
		if not self.is_move:
			return False
		x_prev = self.rect.left
		y_prev = self.rect.top
		self.rect.left += self.speed[0]
		self.rect.top += self.speed[1]
		is_collide = pygame.sprite.spritecollide(self, wall_sprites, False)
		# print('asd',is_collide)
		if gate_sprites is not None:#门没有消失
			if not is_collide:	#没撞到墙
				is_collide = pygame.sprite.spritecollide(self, gate_sprites, False)
				print(is_collide)		#是否撞到门
		if is_collide:#保持原位
			self.rect.left = x_prev
			self.rect.top = y_prev
			return False
		return True